package com.github.postsanf.yinian.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.CreateLabel;
import com.github.postsanf.yinian.bean.Tag;
import com.github.postsanf.yinian.bean.YNAlbum;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.entity.YNAlbumResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.utils.SystemUtils;
import com.github.postsanf.yinian.widget.TagCloudLinkView;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class CreateZoneActivity extends BaseActivity implements View.OnClickListener {
    private EditText et_space;
    private LinearLayout name_label_layout;
    private TagCloudLinkView name_label1;
    private TagCloudLinkView name_label2;
    private TextView create_change;
    private final String labelStr[] = {" # 美食 #", " # 家人 #", " # 旅行 #", " # 毕业 #", " # 校园 #", " # 校园组织 #", " # 聚会 #", " # 好友 #",
            " # 狂欢周末 #", " # 度假 #", " # 自拍 #", " # 节日 #", " # 宝宝 ", " # 公司 #", " # 健身 #", " # 运动 #",
            " # 回忆 #", " # 购物 #", " # 游戏 #", " # 美容美妆 #", " # 伴侣 #", " # 日常 #", " # 萌宠 #", " # 网红 #"};
    String labelsub1[] = {"美食日记", "吃货组合", "美食爱好者"};
    String labelsub2[] = {"全家福", "我们这一家人", "快乐家族"};
    String labelsub3[] = {"旅行的时光", "旅行记忆", "丽江之旅"};
    String labelsub4[] = {"我们毕业了", "毕业旅行"};
    String labelsub5[] = {"校园时光", "校园的日子", "校园记忆"};
    String labelsub6[] = {"学生会", "志愿者协会", "摄影协会"};
    String labelsub7[] = {"我们的聚会", "2017春节聚会", "小聚"};
    String labelsub8[] = {"最佳损友", "我和闺蜜", "我们仨"};
    String labelsub9[] = {"狂欢周末", "周末happy"};
    String labelsub10[] = {"度假之旅", "一起去度假"};
    String labelsub11[] = {"自拍狂组合", "自拍狂魔"};
    String labelsub12[] = {"又一年春节", "中秋团圆", "圣诞狂欢"};
    String labelsub13[] = {"宝宝成长日记", "我们的宝宝", "宝宝相册"};
    String labelsub14[] = {"公司同事", "加班的日子"};
    String labelsub15[] = {"健身记录", "健身啦吼吼吼"};
    String labelsub16[] = {"运动记录"};
    String labelsub17[] = {"青葱岁月", "那些年"};
    String labelsub18[] = {"买买买", "剁手节"};
    String labelsub19[] = {"LOL", "开黑啦"};
    String labelsub20[] = {"爱美是天性"};
    String labelsub21[] = {"我和宝宝"};
    String labelsub22[] = {"点滴生活", "经典语录图集"};
    String labelsub23[] = {"萌宠日常", "逗比小萌宠"};
    String labelsub24[] = {"网红相册"};
    private List<String[]> sublist = new ArrayList<>();
    private List<CreateLabel> CreateLabellist = new ArrayList<>();
    private List<CreateLabel> CreateLabellist2 = new ArrayList<>();
    private boolean ischange = false;
    private boolean isCreating = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_createzone);
        sublist.add(labelsub1);
        sublist.add(labelsub2);
        sublist.add(labelsub3);
        sublist.add(labelsub4);
        sublist.add(labelsub5);
        sublist.add(labelsub6);
        sublist.add(labelsub7);
        sublist.add(labelsub8);
        sublist.add(labelsub9);
        sublist.add(labelsub10);
        sublist.add(labelsub11);
        sublist.add(labelsub12);
        sublist.add(labelsub13);
        sublist.add(labelsub14);
        sublist.add(labelsub15);
        sublist.add(labelsub16);
        sublist.add(labelsub17);
        sublist.add(labelsub18);
        sublist.add(labelsub19);
        sublist.add(labelsub20);
        sublist.add(labelsub21);
        sublist.add(labelsub22);
        sublist.add(labelsub23);
        sublist.add(labelsub24);
        initView();
    }

    private void initView() {
        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.bt_sure).setOnClickListener(this);
        name_label_layout = (LinearLayout) findViewById(R.id.name_label_layout);
        create_change = (TextView) findViewById(R.id.create_change);
        create_change.setOnClickListener(this);
        et_space = ((EditText) findViewById(R.id.et_space));
        et_space.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ClipboardManager cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                if (cmb.getText() != null) {
                    String text = cmb.getText().toString().trim();
                    et_space.setText(text);
                    showToast("粘贴成功");
                }
                return false;
            }
        });
        changelis1();
    }

    int clickNum = 0;
    int clickNum2 = 0;

    private void changelis1() {
        CreateLabellist.clear();
        name_label_layout.removeAllViews();
        View view = LayoutInflater.from(CreateZoneActivity.this).inflate(R.layout.activity_createzone_taglayout, null);
        name_label1 = (TagCloudLinkView) view.findViewById(R.id.name_label);
        for (int j = 0; j < 12; j++) {
            name_label1.add(new Tag(j, labelStr[j]));
            CreateLabel createLabel = new CreateLabel();
            createLabel.setLaber(labelStr[j]);
            createLabel.setSublaber(sublist.get(j));
            CreateLabellist.add(createLabel);
        }
        name_label1.drawTags(R.drawable.create_tag_gray, R.color.color_888888, 12);
        name_label_layout.addView(view);
        name_label1.setOnTagSelectListener(new TagCloudLinkView.OnTagSelectListener() {
            @Override
            public void onTagSelected(Tag tag, int i, View tagView, TextView textView) {
                if (tag.getText().equals(CreateLabellist.get(i).getLaber())) {
                    String[] sub = CreateLabellist.get(i).getSublaber();
                    if (clickNum >= sub.length) {
                        clickNum = 0;
                    }
                    et_space.setText(sub[clickNum]);
                    clickNum++;
                    for (int k = 0; k < name_label1.getTags().size(); k++) {
                        View vi = name_label1.getViews().get(k);
                        vi.setBackgroundResource(R.drawable.create_tag_gray);
                        TextView tv = (TextView) vi.findViewById(R.id.tag_txt);
                        tv.setTextColor(getResources().getColor(R.color.color_888888));
                    }
                    tagView.setBackgroundResource(R.drawable.create_tag_black);
                    textView.setTextColor(getResources().getColor(R.color.white));
                } else {
                    clickNum = 0;
                }
            }
        });
    }

    private void changelis2() {
        CreateLabellist.clear();
        name_label_layout.removeAllViews();
        View view = LayoutInflater.from(CreateZoneActivity.this).inflate(R.layout.activity_createzone_taglayout, null);
        name_label2 = (TagCloudLinkView) view.findViewById(R.id.name_label);
        for (int j = 11; j < 24; j++) {
            name_label2.add(new Tag(j, labelStr[j]));
            CreateLabel createLabel = new CreateLabel();
            createLabel.setLaber(labelStr[j]);
            createLabel.setSublaber(sublist.get(j));
            CreateLabellist2.add(createLabel);
        }
        name_label2.drawTags(R.drawable.create_tag_gray, R.color.color_888888, 12);
        name_label_layout.addView(view);
        name_label2.setOnTagSelectListener(new TagCloudLinkView.OnTagSelectListener() {
            @Override
            public void onTagSelected(Tag tag, int i, View tagView, TextView textView) {
                if (tag.getText().equals(CreateLabellist2.get(i).getLaber())) {
                    String[] sub = CreateLabellist2.get(i).getSublaber();
                    if (clickNum2 >= sub.length) {
                        clickNum2 = 0;
                    }
                    et_space.setText(sub[clickNum2]);
                    clickNum2++;
                    for (int k = 0; k < name_label2.getTags().size(); k++) {
                        View vi = name_label2.getViews().get(k);
                        vi.setBackgroundResource(R.drawable.create_tag_gray);
                        TextView tv = (TextView) vi.findViewById(R.id.tag_txt);
                        tv.setTextColor(getResources().getColor(R.color.color_888888));
                    }
                    tagView.setBackgroundResource(R.drawable.create_tag_black);
                    textView.setTextColor(getResources().getColor(R.color.white));
                } else {
                    clickNum2 = 0;
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_sure:
                String groupName = et_space.getText().toString().trim();
                if (StringUtils.isEmpty(groupName)) {
                    showToast("请输入圈子名称");
                    return;
                }
                if (groupName.length() > 8) {
                    showToast("圈子名称超过长度限制！");
                    return;
                }
                if (SystemUtils.isFastClick()) {
                    Log.i("xjs", "isFastClick");
                    return;
                }
                doPost(groupName);
                break;
            case R.id.create_change:
                if (ischange) {
                    ischange = false;
                    changelis1();
                } else {
                    ischange = true;
                    changelis2();
                }
                break;

        }
    }


    private void doPost(String groupName) {
        if (isCreating) {
            return;
        }
        isCreating = true;
        String UMENG_CHANNEL = SystemUtils.getAppStoreName(CreateZoneActivity.this, "UMENG_CHANNEL");
        String tempTo = getString(R.string.BASE_URL) + getString(R.string.ynCreateAlb);
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_GROUP_NAME, groupName);
        mParam.put(CommonConstants.YNS_GROUP_TYPE, getString(R.string.album_else));
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        mParam.put(CommonConstants.YNS_URL, "");
        mParam.put(CommonConstants.YNS_CHANELSOURCE, UMENG_CHANNEL);
        StringRequest req = new StringRequest(tempTo)
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNAlbumResponse resNewGroup = gson.fromJson(s, YNAlbumResponse.class);
                        if (ReqUtils.isSuccess(resNewGroup.getCode())) {
                            showToast("准备为您创建圈子.......");
                            application.setCurGid(0);//创建新相册时给一个默认的GrupId防止复用上次的GrupId而不能进入新的相册
                            YNAlbum newAlbum = resNewGroup.getData().get(0);
                            newAlbum.setIsInAlbum(1);
                            YNSingleton.getInstance().setCurAlbum(newAlbum);
                            YNSingleton.getInstance().clearStatusList();
                            Intent upIntent = new Intent(ACTIONs.actionAlbumItem);
                            upIntent.putExtra(ACTIONs.aAlbumItem, newAlbum);
                            upIntent.putExtra(ACTIONs.IsAdd, true);
                            sendBroadcast(upIntent);
                            YNSingleton.getInstance().setCurAlbum(newAlbum);
                            application.setCurGid(newAlbum.getGroupid());
                            sendBroadcast(new Intent(ACTIONs.actionUpdateFristAlbum).putExtra("update", true));
                            sendBroadcast(new Intent(ACTIONs.actionToPeopleTimeLine));
                        } else {
                            showToast(resNewGroup.getMsg());
                        }
                        finish();
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }

                    @Override
                    public void onEnd(Response<String> response) {
                        super.onEnd(response);
                        isCreating = false;
                    }
                });
        liteHttp.executeAsync(req);
    }

}
